package com.usian.service.impl;

import com.usian.config.RedisClient;
import com.usian.mapper.TbUserMapper;
import com.usian.pojo.TbUser;
import com.usian.pojo.TbUserExample;
import com.usian.service.SSOService;
import com.usian.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SSOServiceImpl implements SSOService {

    @Autowired
    private TbUserMapper tbUserMapper;

    @Autowired
    private RedisClient redisClient;

    @Value("${USER_INFO}")
    private String USER_INFO;

    @Value("${USER_INTO_EXPIRE}")
    private Long USER_INTO_EXPIRE;

    /**
     * 注册校验
     * @param checkValue
     * @param checkFlag
     * @return
     */
    @Override
    public Boolean checkUserInfo(String checkValue, Integer checkFlag) {
        /* 初始化变量 */
        List<TbUser> list = null;
        /* 构造条件所需对象 */
        TbUserExample example = new TbUserExample();
        TbUserExample.Criteria criteria = example.createCriteria();
        /* 构造条件并查询 */
        if (checkFlag==1){
            criteria.andUsernameEqualTo(checkValue);
            list = tbUserMapper.selectByExample(example);
        }else if (checkFlag==2){
            criteria.andPhoneEqualTo(checkValue);
            list = tbUserMapper.selectByExample(example);
        }

        /* 只有去查询了 并且没查到结果 返回数据 */
        if (list.size()==0){
            return true;
        }
        return false;
    }

    /**
     * 注册
     * @param tbUser
     * @return
     */
    @Override
    public Boolean userRegister(TbUser tbUser) {
        /* 初始化数据 */
        tbUser.setCreated(new Date());
        tbUser.setUpdated(new Date());
        /* 密码加密 */
        tbUser.setPassword(MD5Utils.digest(tbUser.getPassword()));
        /* 添加数据 */
        int i = tbUserMapper.insertSelective(tbUser);
        if (i==1){
            return true;
        }
        return false;
    }

    /**
     * 登陆校验
     * @param username
     * @param password
     * @return
     */
    @Override
    public Map<String, String> userLogin(String username, String password) {
        /* MD5算法加密 */
        String pwd = MD5Utils.digest(password);
        /* 根据username查询user用户 */
        TbUserExample example = new TbUserExample();
        TbUserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        criteria.andPasswordEqualTo(pwd);
        List<TbUser> list = tbUserMapper.selectByExample(example);
        /* 判断是否存在 */
        if (list.size()>0){
            /* 生成token */
            String token = UUID.randomUUID().toString();
            Map<String,String> map = new HashMap<>();
            map.put("token",token);
            map.put("userid",list.get(0).getId().toString());
            map.put("username",list.get(0).getUsername());
            /* 用户信息存储redis */
            redisClient.set(USER_INFO+":"+token,list.get(0));
            redisClient.expire(USER_INFO+":"+token,USER_INTO_EXPIRE);
            return map;
        }
        return null;
    }

    /**
     * 校验用户信息存活状态 存活就刷新redis存活时间
     * @param token
     * @return
     */
    @Override
    public TbUser getUserByToken(String token) {
        TbUser tbUser =(TbUser) redisClient.get(USER_INFO + ":" + token);
        if (tbUser!=null){
            redisClient.expire(USER_INFO + ":" + token,USER_INTO_EXPIRE);
            return tbUser;
        }
        return null;
    }

    @Override
    public Integer logOut(String token) {
        Boolean del = redisClient.del(USER_INFO + ":" + token);
        if (del){
            return 1;
        }
        return null;
    }
}
